#include "fxcc/core/pch.h"
#include "fxcc/core/Dir.h"
#include "fxcc/core/AssertManager.h"

#include "sysx/System.h"
#include <ft2build.h>
#include FT_FREETYPE_H
#include FT_SFNT_NAMES_H


int msdfgen_all()
{
	const std::string _TTfPath = "C:/Windows/Fonts";

	fxcc::Dir dir(_TTfPath);
	for (const auto& file : dir.m_Files)
	{
		if (file->m_FileType == fxcc::_ttf_ttc_) {

			kainjow::mustache::mustache cmdTextTemp = R"(msdf-atlas-gen -font "{{{sourceTTF}}}" -charset "{{{charset}}}" -size {{{fontSize}}} -imageout {{{altasImage}}} -json {{{altasJson}}} -type msdf  -format png
)";
			kainjow::mustache::data d1;
			d1.set("charset", fxcc::AssertManager::GetAbsoluteFile("charset/ascii_charset.txt"));

			const int fontSize = 32;

			d1.set("sourceTTF", file->m_AbsolutePath);
			d1.set("fontSize", std::to_string(fontSize));


			std::string msdfDir = fxcc::AssertManager::GetRelativeFile("msdf");


			std::string filens = fxcc::Base::GetNoSuffixName(file->m_FileName);
			fs::path msdfDirTTF = fs::path(msdfDir) / filens;

			if (!fs::exists(msdfDirTTF))
			{
				fs::create_directories(msdfDirTTF);
			}

			fs::path altasImage = fs::path(msdfDirTTF) / "ascii.png";
			fs::path altasJson = fs::path(msdfDirTTF) / "ascii.json";

			d1.set("altasImage", altasImage.string());
			d1.set("altasJson", altasJson.string());

			std::string cmdText = cmdTextTemp.render(d1);

			std::cout << cmdText << std::endl;
			sysx::System::ExecuteCmd(cmdText);
		}
	}


	return true;
};



struct TTFFile
{
	std::string m_Path;
	std::string m_FamileName;
	std::string m_StyleName;

	TTFFile(const std::string& path) :m_Path(path)
	{
		Init();

	}
	bool Init()
	{
		if (FT_Init_FreeType(&m_Library))
			return false;

		if (FT_New_Face(m_Library, m_Path.c_str(), 0, &m_Face))
		{
			FT_Done_FreeType(m_Library);
			return false;
		}

		m_FamileName = m_Face->family_name;
		m_StyleName = m_Face->style_name;

	}

	FT_Library m_Library;
	FT_Face m_Face;

	~TTFFile()
	{
		FT_Done_Face(m_Face);
		FT_Done_FreeType(m_Library);
	}
	

};


int main()
{
	msdfgen_all();

	const std::string _TTfPath = "C:/Windows/Fonts";

	fxcc::Dir dir(_TTfPath);
	for (const auto& file : dir.m_Files)
	{
		if (file->m_FileType == fxcc::_ttf_ttc_) {

			auto ttfFile = std::make_shared<TTFFile>(file->m_RelativePath);
			std::cout << ttfFile->m_FamileName << "," << ttfFile->m_StyleName << std::endl;
		}
	}
}